load("@io_bazel_rules_go//go:def.bzl", "go_binary")

go_binary(
    name = "adder_archive",
    srcs = ["add.go"],
    cgo = True,
    linkmode = "c-archive",
    tags = ["manual"],
)

cc_test(
    name = "c-archive_test",
    srcs = select({
        "@io_bazel_rules_go//go/platform:windows": ["skip.c"],
        "//conditions:default": ["add_test_archive.c"],
    }),
    deps = select({
        "@io_bazel_rules_go//go/platform:windows": [],
        "//conditions:default": [":adder_archive"],
    }),
)

go_binary(
    name = "c-archive_empty_hdr",
    srcs = ["empty.go"],
    cgo = True,
    linkmode = "c-archive",
    tags = ["manual"],
)

cc_test(
    name = "c-archive_empty_hdr_test",
    srcs = select({
        "@io_bazel_rules_go//go/platform:windows": ["skip.c"],
        "//conditions:default": ["c-archive_empty_hdr_test.c"],
    }),
    deps = select({
        "@io_bazel_rules_go//go/platform:windows": [],
        "//conditions:default": [":c-archive_empty_hdr"],
    }),
)

go_binary(
    name = "adder_shared",
    srcs = ["add.go"],
    cgo = True,
    linkmode = "c-shared",
    tags = ["manual"],
)

cc_test(
    name = "c-shared_test",
    srcs = select({
        "@io_bazel_rules_go//go/platform:windows": ["skip.c"],
        "//conditions:default": ["add_test_shared.c"],
    }),
    deps = select({
        "@io_bazel_rules_go//go/platform:windows": [],
        "//conditions:default": [":adder_shared"],
    }),
)

go_binary(
    name = "crypto",
    srcs = [":crypto.go"],
    cgo = True,
    linkmode = "c-shared",
    tags = ["manual"],
    deps = ["@org_golang_x_crypto//nacl/box:go_default_library"],
)

cc_test(
    name = "c-shared_dl_test",
    srcs = select({
        "@io_bazel_rules_go//go/platform:windows": ["skip.c"],
        "//conditions:default": ["crypto_test_dl.c"],
    }),
    copts = select({
        "@io_bazel_rules_go//go/platform:windows": [],
        "//conditions:default": ['-DSO=\\"$(rootpath :crypto)\\"'],
    }),
    data = select({
        "@io_bazel_rules_go//go/platform:windows": [],
        "//conditions:default": [":crypto"],
    }),
    linkopts = select({
        "@io_bazel_rules_go//go/platform:windows": [],
        "//conditions:default": ["-ldl"],
    }),
)

cc_library(
    name = "adder_sandwich_cc",
    srcs = ["add_sandwich.c"],
    hdrs = ["add_sandwich.h"],
    linkstatic = True,
    alwayslink = True,
)

go_binary(
    name = "adder_sandwich_archive",
    srcs = ["add_sandwich.go"],
    cdeps = [":adder_sandwich_cc"],
    cgo = True,
    linkmode = "c-archive",
    tags = ["manual"],
)

cc_test(
    name = "c-archive_sandwich_test",
    srcs = select({
        "@io_bazel_rules_go//go/platform:windows": ["skip.c"],
        "//conditions:default": ["add_test_sandwich.c"],
    }),
    deps = select({
        "@io_bazel_rules_go//go/platform:windows": [],
        "//conditions:default": [":adder_sandwich_archive"],
    }),
)

go_binary(
    name = "go_with_cgo_dep",
    srcs = select({
        "@io_bazel_rules_go//go/platform:windows": ["empty.go"],
        "//conditions:default": ["go_with_cgo_dep.go"],
    }),
    cgo = True,
    linkmode = "c-archive",
    deps = ["@org_golang_x_sys//unix"],
)

cc_binary(
    name = "go_with_cgo_dep_caller",
    srcs = select({
        "@io_bazel_rules_go//go/platform:windows": ["skip.c"],
        "//conditions:default": ["go_with_cgo_dep_caller.cc"],
    }),
    deps = [":go_with_cgo_dep"],
)
